﻿using System;
using System.Windows.Forms.Design;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Services;
using Microsoft.Practices.RecipeFramework.Configuration;
using Microsoft.Practices.RecipeFramework.Library;
using com.Editors;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;

namespace com.code.Actions
{
    public class MainAction : Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.T4Action
    {
        #region Input Properties

        private EnvDTE.Project webProject;
        private string targetWebCodeFile;
        private string generateWebCodeContent;
        private string targetWebFile;
        private string generateWebPageContent;
        private string targetCommonFile;
        private string generateCommonClassContent;
        private EnvDTE.Project commonProject;
        private string targetSPFile;
        private string generateSPContent;
        private EnvDTE.Project dALProject;
        private string targetDALFile;
        private ArrayList dataStr;

        [Input(Required = false)]
        public ArrayList DataStr
        {
            get { return dataStr; }
            set { dataStr = value; }
        }

        private string generateDALClassContent;

        [Input(Required = false)]
        public string GenerateDALClassContent
        {
            get { return generateDALClassContent; }
            set { generateDALClassContent = value; }
        }

        [Input(Required = false)]
        public string TargetDALFile
        {
            get { return targetDALFile; }
            set { targetDALFile = value; }
        }

        [Input(Required = false)]
        public EnvDTE.Project DALProject
        {
            get { return dALProject; }
            set { dALProject = value; }
        }
        
        [Input(Required = false)]
        public string GenerateSPContent
        {
            get { return generateSPContent; }
            set { generateSPContent = value; }
        }

        [Input(Required = false)]
        public string TargetSPFile
        {
            get { return targetSPFile; }
            set { targetSPFile = value; }
        }
        
        [Input(Required = false)]
        public EnvDTE.Project CommonProject
        {
            get { return commonProject; }
            set { commonProject = value; }
        }

        [Input(Required = false)]
        public string GenerateCommonClassContent
        {
            get { return generateCommonClassContent; }
            set { generateCommonClassContent = value; }
        }

        [Input(Required = false)]
        public string TargetCommonFile
        {
            get { return targetCommonFile; }
            set { targetCommonFile = value; }
        }
        
        [Input(Required = false)]
        public string GenerateWebPageContent
        {
            get { return generateWebPageContent; }
            set { generateWebPageContent = value; }
        }

        [Input(Required = false)]
        public string TargetWebFile
        {
            get { return targetWebFile; }
            set { targetWebFile = value; }
        }
        
        [Input(Required = false)]
        public string GenerateWebCodeContent
        {
            get { return generateWebCodeContent; }
            set { generateWebCodeContent = value; }
        }

        [Input(Required = false)]
        public string TargetWebCodeFile
        {
            get { return targetWebCodeFile; }
            set { targetWebCodeFile = value; }
        }

        [Input(Required = false)]
        public EnvDTE.Project WebProject
        {
            get { return webProject; }
            set { webProject = value; }
        }
        #endregion

        #region IAction Members

        public override void Execute()
        {
            
            IUIService uiService = GetService<IUIService>(true);
            if (uiService != null)
            {
                TableDataForm form = new TableDataForm();
                if (this.TargetSPFile != string.Empty)
                    form.addOutputTabs(this.TargetSPFile, this.GenerateSPContent);
                if (this.TargetDALFile != string.Empty)
                    form.addOutputTabs(this.TargetDALFile, this.GenerateDALClassContent);
                if (this.TargetCommonFile != string.Empty)
                    form.addOutputTabs(this.TargetCommonFile, this.GenerateCommonClassContent);
                if (this.TargetWebCodeFile != string.Empty)
                    form.addOutputTabs(this.TargetWebCodeFile, this.GenerateWebCodeContent);
                if (this.TargetWebFile != string.Empty)
                    form.addOutputTabs(this.TargetWebFile, this.GenerateWebPageContent);
                //IActionExecutionService execucteService = GetService<IActionExecutionService>(true);
                //HelloWorldAction action = new HelloWorldAction();
                //action.HelloMessage = "Test it";
                //execucteService.Execute(action.Site.Name);
                form.addOutputTabs("HellAction", GenerateTextFile(DataStr, "TestModule"));
                form.Show();
                //uiService.ShowMessage(helloMessage, "Hello Action");
            }
            else
            {
                //System.Windows.Forms.MessageBox.Show(helloMessage, "Hello Action");
            }            

        }

        public override void Undo()
        {
        }

        private string GenerateTextFile(ArrayList list, string module)
        {
            string data = "";
            using (Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TextTemplateAction myAction = new Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.TextTemplateAction())
            {
                
                StringDictionary dic = new StringDictionary();
                dic.Add("DataStr", "Data Str");
                dic.Add("ModuleName", "Testing.....");
                //myAction.Configure(dic);
                // set parameters for action  
                //myAction.Content = content;
                myAction.Template = @"Text/BindCode.cs.t4";
                TypeDescriptor.GetProperties(myAction)["DataStr"].SetValue(myAction, list);
                TypeDescriptor.GetProperties(myAction)["ModuleName"].SetValue(myAction, module);
                //base.additionalArguments.Add("DataStr", "Data Str");
                //base.additionalArguments.Add("ModuleName", "Testing.....");
                base.Container.Add(myAction);
                
                myAction.Execute();
                data = myAction.Content; 

            }
            return data;
        }

        
        #endregion
    }
}
